package com.youthfilmic.yuejian.dao;

import com.youthfilmic.yuejian.domain.Application;
import com.youthfilmic.yuejian.domain.Certification;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by youweixi on 15/10/7.
 */
public interface ApplicationDao extends CrudRepository<Application , Long> {

    /**
     * 根据id来更新数据
     * */
    @Modifying
    @Transactional
    @Query("update Application a set a.status = ?2 where a.id = ?1")
    int modifyStatusById( Long id , int status);

    /**
     * 当状态未指定状态的时候才更新
     * */
    @Modifying
    @Transactional
    @Query("update Application a set a.status = ?3 where a.id = ?1 and a.status=?2")
    int modifyStatusByIdAndStatus( Long id , int ostatus , int tstatus);

    Page<Application> findByUserId(Long userId , Pageable pageable);


    @Query("select application from Application application where (application.status = 0 or application.status = 1) and application.userId = ?1")
    Page<Application> findByUserIdForRunning(Long userId , Pageable pageable);

    @Query("select application from Application application where application.status = 2 and application.userId = ?1")
    Page<Application> findByUserIdForComment(Long userId , Pageable pageable);

    @Query("select application from Application application where (application.status > 2 or application.status < 0) and application.userId = ?1")
    Page<Application> findByUserIdForCompeted(Long userId , Pageable pageable);

    /**
     * 只需要看到已经支付的申请即可
     * */
    @Query("select application from Application application where application.status = 1 and application.starId = ?1")
    Page<Application> findByStarIdForRunning(Long starId , Pageable pageable);

    /**
     * 申请通过的，等待评论的
     * */
    @Query("select application from Application application where application.status = 2 and application.starId = ?1")
    Page<Application> findByStarIdForComment(Long starId , Pageable pageable);

    /**
     * 成功地或者拒绝的
     * */
    @Query("select application from Application application where (application.status > 2 or application.status < 0) and application.starId = ?1")
    Page<Application> findByStarIdForCompeted(Long userId , Pageable pageable);

    Page<Application> findAll(Pageable pageable);

}
